package ltu.thole;

import java.util.List;

import android.net.wifi.ScanResult;

public class Device {
	
	private String name = "No name";
	private String address = "";
	private int port = 8312;
	private WiFiRoughlyPosition position = null;
	private List<ScanResult> scanResults = null;
	private String macAddress = "";
	private static int deviation = 14;
	
	public Device(String name, String address, int port) {
		this.name = name;
		this.address = address;
		this.port = port;
	}
	
	public Device(String name, String address, int port, WiFiRoughlyPosition position) {
		this.name = name;
		this.address = address;
		this.port = port;
		this.position = position;
	}
	
	public void addPosition(WiFiRoughlyPosition position) {
		this.position = position;
	}
	
	public void addMacAddress(String macAddress) {
		this.macAddress = macAddress;
	}
	
	public String getName() {
		return name;
	}
	
	public String getAddress() {
		return address;
	}
	
	public int getPort() {
		return port;
	}
	
	public WiFiRoughlyPosition getPosition() {
		return position;
	}
	
	public List<ScanResult> getScanResult() {
		return scanResults;
	}

	public double compareTo(List<ScanResult> another) {

		int matchingMacAddresses = 0;
		int matchingStrengths = 0;
		
		double ratioMacAddresses;
		double ratioStrength = 0;
		int currentStrength;
		int deviceStrength;

		// Go through all the current scan results.
		for (int i = 0; i < another.size(); i++) {
			
			// Go through all the scan results for this device.
			for (int j = 0; j < scanResults.size(); j++) {
				// If a matching MAC-address is found.
				if (another.get(j).BSSID.contains(scanResults.get(i).BSSID)) {
					//Matching MAC-addresses.
					matchingMacAddresses++;
					currentStrength = another.get(i).level;
					deviceStrength = scanResults.get(j).level;
					// Check if the signal strength matches.
					if ((currentStrength >= (deviceStrength - deviation)) && (currentStrength <= (deviceStrength + deviation))) {
						// Signal strength is within margin of error.
						matchingStrengths++;
						if (currentStrength > deviceStrength) {
							ratioStrength += (Double.parseDouble(""+(currentStrength*-1))/Double.parseDouble(""+(deviceStrength*-1)));
							//Toast.makeText(this, ("ratioStrength: " + ratioStrength + "(" + currentStrength + ", " + deviceStrength + ")"), Toast.LENGTH_LONG).show();
						}
					}
				}
			}
		}
		
		ratioMacAddresses = (Double.parseDouble(""+(matchingMacAddresses)/Double.parseDouble(""+(scanResults.size()))));
		ratioStrength = (ratioStrength/Double.parseDouble(""+(matchingMacAddresses)));
		//Toast.makeText(this, ("ratioMacAddresses: " + ratioMacAddresses + "\nratioStrength: " + ratioStrength), Toast.LENGTH_LONG).show();
		//Toast.makeText(this, ("Ratio: " + ((ratioStrength*ratioMacAddresses)*100) + "%"), Toast.LENGTH_LONG).show();
		//Toast.makeText(this, ("matchingMacAddresses: " + matchingMacAddresses + "\nmatchingStrengths: " + matchingStrengths), Toast.LENGTH_LONG).show();
		//proximityList.add(new DeviceProximity(deviceList.get(i), ratioStrength));
		
		return ratioStrength;
	}

}
